package com.tzstcl.sys.user.ctrl;

import com.github.pagehelper.PageInfo;
import com.tzstcl.base.ctrl.BaseCtrl;
import com.tzstcl.base.model.AjaxResult;
import com.tzstcl.commons.utils.StringUtils;
import com.tzstcl.sys.user.model.SysRole;
import com.tzstcl.sys.user.model.SysRolePermission;
import com.tzstcl.sys.user.service.SysRolePermissionService;
import com.tzstcl.sys.user.service.SysRoleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公司：天筑科技股份有限公司
 * 作者：guohy
 * 日期：2019年11月08日
 * 说明：角色表Controller
 */
@Controller
@RequestMapping("/admin/sysRole")
public class SysRoleCtrl extends BaseCtrl {

    @Autowired
    private SysRoleService sysRoleService;
	@Autowired
    private SysRolePermissionService sysRolePermissionService;

    /**
    * list页面导航
    * @return
    */
    @RequiresPermissions("sysRole:view")
    @RequestMapping("/toList")
    public String toList() {
        return "admin/sys/user/sysRoleList";
    }

    /**
    * 获取查询的分页数据
    * @param sysRole
    * @return
    */
    @RequestMapping("/list")
    @RequiresPermissions("sysRole:view")
    @ResponseBody
    public PageInfo<SysRole> list(SysRole sysRole) {
        return  sysRoleService.selectPage(sysRole);
    }

    /**
    * form页面导航
    * @return
    */
    @RequiresPermissions(value={"sysRole:edite","sysRole:add"},logical=Logical.OR)
    @RequestMapping("/toForm")
    public String toForm(@RequestParam(value="id", required=false)  Long id, Model model) {
        if(null != id){
            model.addAttribute("sysRole" ,sysRoleService.getOne(id));
        }
        return "admin/sys/user/sysRoleForm";
    }

    /**
    * form页面导航 详情
    * @return
    */
    @RequiresPermissions("sysRole:view")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id")Long id, Model model) {
        if(null != id){
            model.addAttribute("sysRole" ,sysRoleService.getOne(id));
        }
        return "admin/sys/user/sysRoleDetail";
    }

    /**
     * 新增
     * @param sysRole
     * @return
     */
    @RequestMapping("/add")
    @RequiresPermissions(value={"sysRole:add"})
    @ResponseBody
    public AjaxResult save(SysRole sysRole) {
        if (null != sysRole.getPermissionIds() && sysRole.getPermissionIds().size() > 0) {
            sysRoleService.save(sysRole);
        }
        else {
            return error("权限列表不能为空");
        }
        return AjaxResult.success("success");
    }

    /**
    * form页面导航 更新
    * @return
    */
    @RequiresPermissions("sysRole:edit")
    @GetMapping("/edit/{id}")
    public String toUpdate(@PathVariable("id")Long id, Model model) {
        if(null != id){
            SysRole sysRole = sysRoleService.getOne(id);
            SysRolePermission roleMenu = new SysRolePermission();
            roleMenu.setRoleId(sysRole.getId());
            List<SysRolePermission> list = sysRolePermissionService.selectList(roleMenu);
            List<String> permissionIds = new ArrayList<>();
            for (SysRolePermission rolePermission : list) {
                permissionIds.add(rolePermission.getPermissionId().toString());
            }
            model.addAttribute("sysRole" ,sysRole);
            model.addAttribute("permissionIds" ,permissionIds);
        }
        return "admin/sys/user/sysRoleEdit";
    }

    /**
    * 更新
    * @param sysRole
    * @return
    */
    @PostMapping("/update")
    @RequiresPermissions("sysRole:edit")
    @ResponseBody
    public AjaxResult update(@Valid SysRole sysRole) {
        if (null != sysRole.getPermissionIds() && sysRole.getPermissionIds().size() > 0) {
            sysRoleService.save(sysRole);
        }
        else {
            return error("权限列表不能为空");
        }
        return success("更新成功");
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sysRole:delete")
    @ResponseBody
    public AjaxResult delete(String ids) {
        return toAjax(sysRoleService.deleteBatchIds(ids));
    }

    /**
     * 获取单条信息
     * @param id
     * @return
     */
    @RequestMapping("/get")
    @RequiresPermissions("sysRole:view")
    @ResponseBody
    public AjaxResult get(Long id) {
        return  success("获取信息成功",sysRoleService.getOne(id));
    }

    /**
    * 校验唯一性
    * 权限配置为：多权限任选一，有新增和修改其一权限就可以访问
    * @param sysRole
    * @return
    */
    @PostMapping("/checkUnique")
    @RequiresPermissions(value={"sysRole:add", "sysRole:edit"},logical=Logical.OR)
    @ResponseBody
    public Map<String,Boolean> checkUnique(SysRole sysRole) {
        Map<String,Boolean> result = new HashMap<String,Boolean>(4);
        result.put("valid",true);
        Long id = sysRole.getId();
        sysRole.setId(null);
        List<SysRole> sysRoleList = sysRoleService.selectList(sysRole);
        if(StringUtils.isNotEmpty(sysRoleList)){
            if(sysRoleList.size()>1){
                result.put("valid",false);
            }else{
                if(id != null){
                    // 新增
                    result.put("valid",false);
                }else{
                    // 修改
                    if(!id.equals(sysRoleList.get(0).getId())){
                    result.put("valid",false);
                    }
                }
            }
        }
        return result;
    }

    /**
     * 获取角色列表
     *
     * @param sysRole
     * @return
     */
    @RequestMapping(value = "/roleList")
    @ResponseBody
    public List<SysRole> findInfo(SysRole sysRole) {
        return sysRoleService.selectList(sysRole);
    }

    @RequestMapping(value = "/deleteOne")
    @ResponseBody
    @RequiresPermissions("sysRole:delete")
    public AjaxResult deleteOne(SysRole sysRole) {
        return toAjax(sysRoleService.deleteOne(sysRole));
    }

}